package com.cy.ppj.service.mall.impl;

import com.cy.ppj.dao.mapper.mall.GoodsCategoryMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.mall.GoodsCategory;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.GoodsCategoryAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsCategoryAdminListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.mall.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 商品类别 service
 * @Author tanyilin
 * @Date 2019/7/3 11:56
 */
@Service
public class GoodsCategoryServiceImpl extends BaseService implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    @Override
    public PageBean<GoodsCategoryAdminListVO> getGoodsCategoryAdminList(GoodsCategoryAdminListQuery query) {

        int total = goodsCategoryMapper.selectGoodsCategoryAdminListCount(query);
        List<GoodsCategoryAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = goodsCategoryMapper.selectGoodsCategoryAdminList(query);
            list.stream().forEach(c -> {
                c.setStatusText(c.getStatus().getText2());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    @Transactional
    public void addGoodsCategory(GoodsCategory insertGoodsCategory) {

        Date time = new Date();
        insertGoodsCategory.setWeight(goodsCategoryMapper.selectMaxWeight() + 1);
        insertGoodsCategory.setStatus(CommonStatus.ENABLE);
        insertGoodsCategory.setCreateTime(time);
        insertGoodsCategory.setUpdateTime(time);
        goodsCategoryMapper.insertSelective(insertGoodsCategory);
    }

    @Override
    @Transactional
    public void updateGoodsCategory(GoodsCategory updateGoodsCategory) {

        Date time = new Date();
        updateGoodsCategory.setUpdateTime(time);
        goodsCategoryMapper.updateByPrimaryKeySelective(updateGoodsCategory);
    }

    @Override
    public List<GoodsCategory> getAllGoodsCategoryList() {

        List<GoodsCategory> list = goodsCategoryMapper.selectAllGoodsCategoryList();
        return list;
    }


}
